﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace OgrIsleri.DashBoard.Modul.Notlar
{
    public partial class Not : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            VeriDoldur();
        }
        private void VeriDoldur()
        {
            using (DbEntities db = new DbEntities())
            {
                Guid AkID = (Guid)Membership.GetUser().ProviderUserKey;
                var data = db.tblDersProgramis.Where(o => o.AkademisyenID == AkID).Select(o=>o.tblSiniflar).Distinct().ToList();
 
                foreach (var a in data)
                {
                     DpSiniflar.Items.Add(new ListItem() { Text =a.SinifAdi, Value = a.SinifID.ToString() });
                }  
            }
        }
        protected void BtnKaydet_Click(object sender, EventArgs e)
        {
            using (DbEntities db = new DbEntities())
            {
               int SinifID = Convert.ToInt32(DpSiniflar.SelectedValue);
               int DersID=Convert.ToInt32(DpDersler.SelectedValue);
               var data = db.tblUyeDetaylaris.Where(o => o.SinifID == SinifID).ToList();
               RptListe.DataSource = data;
               RptListe.DataBind();
            }
        }

        protected void DpSiniflar_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DbEntities db = new DbEntities())
            {
                Guid AkID = (Guid)Membership.GetUser().ProviderUserKey;
                int SinifID = Convert.ToInt32(DpSiniflar.SelectedValue);
                var data = db.tblDersProgramis.Where(o => o.AkademisyenID == AkID && o.SinifID==SinifID).Select(o => o.tblDersler).Distinct().ToList();

                foreach (var a in data)
                {
                    DpDersler.Items.Add(new ListItem() { Text = a.DersAdi, Value = a.DersID.ToString() });
                }
            }
        }

        protected void RptListe_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            using (DbEntities db = new DbEntities())
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    HiddenField username = (HiddenField)e.Item.FindControl("HdUserID");
                    TextBox vize = (TextBox)e.Item.FindControl("txtVize");
                    TextBox final = (TextBox)e.Item.FindControl("txtFinal");
                    TextBox butunleme = (TextBox)e.Item.FindControl("txtButunleme");
                    Guid ogrenci = Guid.Parse(username.Value);

                    int DersID = Convert.ToInt32(DpDersler.SelectedValue);

                    var data = db.tblNotlars.Where(o => o.OgrenciID == ogrenci && o.DersID == DersID).SingleOrDefault();
                    if (data != null)
                    {
                        vize.Text = data.Vize != null ? data.Vize.ToString() : "0";
                        final.Text = data.Final != null ? data.Final.ToString() : "0";
                        butunleme.Text = data.Butunleme != null ? data.Butunleme.ToString() : "0";
                    }
                    else
                    {
                        vize.Text = final.Text = butunleme.Text = "0";
                    }
                }


            }
        }

        protected void BtnNot_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in RptListe.Items)
            {
                using (DbEntities db = new DbEntities())
                {

                    HiddenField username = (HiddenField)item.FindControl("HdUserID");
                    TextBox vize = (TextBox)item.FindControl("txtVize");
                    TextBox final = (TextBox)item.FindControl("txtFinal");
                    TextBox butunleme = (TextBox)item.FindControl("txtButunleme");
                    Guid ogrenci = Guid.Parse(username.Value);

                    int DersID = Convert.ToInt32(DpDersler.SelectedValue);

                    var data = db.tblNotlars.Where(o => o.OgrenciID == ogrenci && o.DersID == DersID).SingleOrDefault();
                    if (data != null)
                    {
                        data.Vize = Convert.ToInt32(vize.Text);
                        data.Final = Convert.ToInt32(final.Text);
                        data.Butunleme = Convert.ToInt32(butunleme.Text);
                    }
                    else
                    {
                        tblNotlar veri = new tblNotlar();
                        veri.Vize = Convert.ToInt32(vize.Text);
                        veri.Final = Convert.ToInt32(final.Text);
                        veri.Butunleme = Convert.ToInt32(butunleme.Text);
                        veri.DersID = DersID;
                        veri.OgrenciID = ogrenci;
                        db.tblNotlars.Add(veri);
                    }
                    db.SaveChanges();
                }
            }
            Response.Redirect("Not.aspx");
        }
    }
}